#include <gl\glew.h>
#include <gl\freeglut.h>

//Global definitions----------------------------------------
bool glob_IsKeyDown[256];

//window
int glob_WindowPositionX = 50,
	glob_WindowPositionY = 50,
	glob_WindowWidth = 400,
	glob_WindowHeight = 400;
//-------

//for reshape function
double glob_AngleOfView_degs = 60,
	glob_NearPlaneDist = 1,
	glob_FarPlaneDist = 10000;
//-------

//camera
double glob_CameraPosX = 30,
	glob_CameraPosY = 30,
	glob_CameraPosZ = 30,
	glob_CameraTargetX = 0,
	glob_CameraTargetY = 0,
	glob_CameraTargetZ = 0,
	glob_WorldUpX = 0,
	glob_WorldUpY = 1,
	glob_WorldUpZ = 0;
//-------

//teapot
double glob_TeapotRotationY_degs = 0.0;

//-----------------------------------------------------------


void Reshape(int width, int height)
{
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	double aspectRatio = (double)width / (double)height;

	gluPerspective(glob_AngleOfView_degs, aspectRatio, glob_NearPlaneDist, glob_FarPlaneDist);
	glMatrixMode(GL_MODELVIEW);
}

void DrawTeapot(double size, 
	double locationX, double locationY, double locationZ,
	double rotationX_degs, double rotationY_degs, double rotationZ_degs,
	double colorRed, double colorGreen, double colorBlue)
{
	glPushMatrix();

	glTranslatef(locationX, locationY, locationZ);
	glRotatef(rotationX_degs, 1.0, 0.0, 0.0);
	glRotatef(rotationY_degs, 0.0, 1.0, 0.0);
	glRotatef(rotationZ_degs, 0.0, 0.0, 1.0);

	glColor4f(colorRed, colorGreen, colorBlue, 1.0);

	glutSolidTeapot(size);

	glPopMatrix();
}

void DrawObjects()
{
	DrawTeapot(5, 
		0, 0, 0, 
		0, glob_TeapotRotationY_degs, 0, 
		1.0, 1.0, 1.0);
}

void ApplyCamera()
{
	gluLookAt(glob_CameraPosX, glob_CameraPosY, glob_CameraPosZ,
		glob_CameraTargetX, glob_CameraTargetY, glob_CameraTargetZ,
		glob_WorldUpX, glob_WorldUpY, glob_WorldUpZ);

}

void Display()
{
	/*
	if(glob_IsKeyDown['b'] || glob_IsKeyDown['B'])
	{
		glClearColor(0.0, 0.0, 1.0, 1.0);
	}
	else
	{
		glClearColor(0.0, 0.0, 0.0, 1.0);
	}
	*/
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	ApplyCamera();
	DrawObjects();

	glutSwapBuffers();
}

void HandleKeyboard()
{
	if(glob_IsKeyDown['r'] || glob_IsKeyDown['R'])
	{
		glob_TeapotRotationY_degs += 2;
	}
	if(glob_IsKeyDown['e'] || glob_IsKeyDown['E'])
	{
		glob_TeapotRotationY_degs -= 2;
	}
}

void GameLoop()
{
	HandleKeyboard();
	Display();
}

void KeyDown(unsigned char key, int cursorPosX, int cursorPosY)
{
	glob_IsKeyDown[key] = true;
}

void KeyUp(unsigned char key, int cursorPosX, int cursorPosY)
{
	glob_IsKeyDown[key] = false;
}

void InitializeWindow()
{
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowPosition (glob_WindowPositionX, glob_WindowPositionY);
	glutInitWindowSize (glob_WindowWidth, glob_WindowHeight);
	glutCreateWindow("OpenGL Basics");
}

void RegisterFunctions()
{
	glutDisplayFunc(Display);
	glutReshapeFunc(Reshape);
	glutIdleFunc(GameLoop);
	glutKeyboardFunc(KeyDown);
	glutKeyboardUpFunc(KeyUp);
}

void InitializeGL()
{
	glEnable(GL_DEPTH);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_COLOR_MATERIAL);
}

int main(int argc, char ** argv)
{
	glutInit(&argc, argv);
	InitializeWindow();
	RegisterFunctions();
	InitializeGL();

	glutMainLoop();
	return 0;
}